#include "PrecompiledHeader.h"
#include "CRenderer.h"

/*--------------------------------------------------------------------------*/
CRenderer* g_Renderer = NULL;
/*--------------------------------------------------------------------------*/

CRenderer::CRenderer() :
	IEngMod("CRenderer")
{
	AssertModule(g_Shell);
	g_Shell->RegisterLuaFunction("screenshot", sh::screenshot);

	ViaMain::CallOnLoop(this, 0, 70, "RenderAll");
	ViaMain::CallOnLoop(this, 1, 1, "FlipFrame");
	ViaMain::CallOnLoop(this, 2, 2, "Update UBOs");

	this->UpdateUBOs();
}

/*--------------------------------------------------------------------------*/

CRenderer::~CRenderer()
{

}

/*--------------------------------------------------------------------------*/

void CRenderer::OnLoop(const uint i)
{
	switch (i)
	{
	case 0:
	{
// 		ROpenGL::ClearBuffers();
// 		static Font* font = NULL;
//
// 		if (font == NULL)
// 		{
// 			g_Resources->Get("default_big", &font);
// 		}
//
// 		static TextureRes* texture = NULL;
//
// 		if (texture == NULL)
// 		{
// 			g_Resources->Get("default", &texture);
// 		}
//
// 		g_Renderer->DrawRectangle2D(glm::vec2(100, 200), glm::vec2(200, 300), glm::vec2(300, 500), glm::vec2(150, 450), NULL);
// 		g_Renderer->DrawRectangle2D(glm::vec2(400, 400), glm::vec2(500, 500), NULL);
// 		g_Renderer->DrawRectangle2D(glm::vec2(100, 100), glm::vec2(200, 200), texture->GetTexture());
// 		g_Renderer->DrawTriangle2D(glm::vec2(400, 400), glm::vec2(200, 380), glm::vec2(250, 600), texture->GetTexture());
// 		g_Renderer->DrawLine2D(glm::vec2(100, 500), glm::vec2(700, 100), texture->GetTexture());
//
//
// 		g_Renderer->DrawText2D(font, "To jest napis", glm::vec2(100, 100), Color::White, EFontStyle::Outline);
// 		g_Renderer->DrawText2D(font, "Jakis inny napis", glm::vec2(100, 300), Color::Pink, EFontStyle::Outline);
// 		g_Renderer->DrawText2D(font, "Inny napis", glm::vec2(300, 200), Color::Green);
// 		g_Renderer->DrawText2D(font, "Inny napis2", glm::vec2(200, 400), Color::Yellow);
		//this->RenderFrame();
		//this->BenchmarkLog("CRenderer::RenderFrame");

		this->ProcessPhases();
		break;
	}

	case 1:
	{
		this->SwapBuffers();
		break;
	}

	case 2:
	{
		this->UpdateUBOs();

		break;
	}
	}
}

/*--------------------------------------------------------------------------*/

void sh::screenshot()
{
	time_t sectime;
	struct tm timeinfo;
	time(&sectime);

	localtime_s(&timeinfo, &sectime);

	std::stringstream ss;
	ss << "../Screenshots/" << (1900 + timeinfo.tm_year) << "-"
	   << std::setfill('0') << std::setw(2) << timeinfo.tm_mon << "-"
	   << std::setfill('0') << std::setw(2) << timeinfo.tm_mday << "-"
	   << std::setfill('0') << std::setw(2) << timeinfo.tm_hour << "-"
	   << std::setfill('0') << std::setw(2) << timeinfo.tm_min << "-"
	   << std::setfill('0') << std::setw(2) << timeinfo.tm_sec << ".bmp";

	AssertModule(g_Renderer);
	g_Renderer->SaveScreenshot(ss.str());
}

/*--------------------------------------------------------------------------*/